package com.hongdee.scm.pushMessage.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.scm.pushMessage.entity.TScmMapToJSON;
import com.hongdee.scm.pushMessage.service.ITScmPushMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("scm/pushMsg")
public class TScmPushMsgController {

    @Autowired
    private ITScmPushMsgService itScmPushMsgService;

    /**
     * 短信推送
     * @param uid
     * @param token
     * @param text
     * @param map
     * @param saId
     * @return
     * @throws Exception
     */
    @RequestMapping("/pushMsg")
    @ResponseBody
    public ResultEntity pushMsg(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "text", defaultValue = "") String text,
                                @RequestBody Map map,
                                @RequestBody List<Map<String,Object>> saId) throws Exception {

        String title= TScmMapToJSON.obj2JsonString(map);
        //type:1 调拨审批
        itScmPushMsgService.push(saId, title,"1");
        ResultEntity re = new ResultEntity();
        re.setSuccess(true);
        re.setMsg("消息推送成功!!");
        return re;
    }

    /**
     * 消息中心
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/msgCenter")
    @ResponseBody
    public Map msgCenter(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token
    ) throws Exception {

        List list = this.itScmPushMsgService.messageCenter(uid);
        Map map = new HashMap();
        map.put("msg","查询成功！");
        map.put("success",true);
        map.put("dataList",list);
        return map;
    }

    /**
     * 短信定时发送
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/sendMsg")
    @ResponseBody
    public Map sendMsg(@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "token", defaultValue = "") String token
    ) throws Exception {

        this.itScmPushMsgService.sendMessage();
        Map map = new HashMap();
        map.put("msg","发送成功！");
        map.put("success",true);
        return map;
    }

    /**
     * 定时查看销售单是否有公司，没有就发短信
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/sendSaleMsg")
    @ResponseBody
    public Map sendWarningMsg(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token
    ) throws Exception {
        //定时查看销售单是否有公司，没有就发短信
        this.itScmPushMsgService.saleInvoiceWarn();
        //定时查看出库的车辆，是否财务也出库了
        this.itScmPushMsgService.ifInvoiceInsert();
        Map map = new HashMap();
        map.put("msg","发送成功！");
        map.put("success",true);
        return map;
    }
}
